<template>

<div class="sysuser" style="width: 99%">
      <!-- 供应商用户创建 -->
        <!-- 查询表单 -->
    <el-form ref="form" style="padding-left:15px;" :model="form" :inline="true" :rules="rules" size="mini">
     <!-- <el-row :gutter="12" class="row">
      <el-col :span="5"> -->
      <el-form-item
        label="供应商名称："
        prop="cmpName"
      >
       <el-input v-model="form.cmpName" clearable style="width:180px"></el-input>
      </el-form-item>
      <!-- </el-col> -->
      <!-- <el-col :span="5"> -->
      <el-form-item label="管理员名称：" prop="usrName">
        <el-input v-model="form.usrName" style="width:180px" clearable ></el-input>
      </el-form-item>
      <!-- </el-col> -->
      <!-- <el-col :span="2"> -->
      <el-form-item size="mini">
        <el-button type="primary" @click="submitForm('form')">查询</el-button>
      </el-form-item>
      <!-- </el-col> -->
      <!-- </el-row> -->
    </el-form>
     <!-- 主表操作按钮 -->
    <div class="btn-area" ref="detlArea">
      <buttonName @clickEvent="clickEventDom"></buttonName>
<!--        <el-button plain size="mini" type="primary" v-if="isAuth('add')" @click="add" >新增</el-button>-->
<!--        <el-button plain size="mini" type="primary" v-if="isAuth('edit')" @click="edit" >编辑</el-button>-->
<!--        <el-button plain size="mini" type="primary" v-if="isAuth('del')" @click="del" >删除</el-button>-->
<!--        <el-button plain size="mini" type="primary" v-if="isAuth('restPassword')" @click="resetpwd">密码重置</el-button>-->
      </div>
    <!-- table表单 -->
    <div class="sysuser-table">
      <!-- 主表数据展示 -->
      <!-- @row-click="" -->
      <!-- @row-dblclick='' -->
      <div class="inner" ref="inner">
      <el-table
        :data="MainTableData"
        style="width:100%;height:100%"
        border
        :height="maxHeight"
        :header-cell-style="{ textAlign: 'center' }"
        highlight-current-row
        @row-click="rowClick"
        >
        <el-table-column type="index" :index="indexMethod" align="center" label="序号" width="50"></el-table-column>
        <el-table-column prop="cmpCode" label="供应商账号"  show-overflow-tooltip></el-table-column>
        <el-table-column prop="cmpName" label="供应商名称"  show-overflow-tooltip></el-table-column>
        <el-table-column prop="usrCode" label="管理员账号"  show-overflow-tooltip></el-table-column>
        <el-table-column prop="usrName" label="管理员名称"  show-overflow-tooltip></el-table-column>
        <el-table-column prop="usrCellphone" label="联系电话" show-overflow-tooltip></el-table-column>
        <el-table-column prop="usrEmail" label="邮箱" show-overflow-tooltip width="120px"></el-table-column>
        <el-table-column prop="usrStatus" label="状态" show-overflow-tooltip align="center" width="100px">
          <template slot-scope="scope">
          <el-checkbox
          :value="scope.row.usrStatus=='Y'?true:false"
          :disabled="true"></el-checkbox>
          </template>
        </el-table-column>
      </el-table>
         <!-- 分页 -->
      <div>
        <el-pagination
          class="pagination"
          small
          background
          ref="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="Pagination.currentPage"
          :page-sizes="Pagination.pageSizeList"
          :page-size="Pagination.pageSize"
          :layout="Pagination.layout"
          :total="Pagination.total"
        ></el-pagination>
      </div>
      </div>
    </div>

    <!-- 弹出对话框 -->
    <el-dialog v-el-drag-dialog
               :title="title" :visible.sync="dialogFormVisible" :close-on-click-modal="false" :destroy-on-close="true">
      <add-dialog v-if='mode' @cancel='cancel' @save='save'></add-dialog>
      <edit-dialog v-else @cancel='cancel' @edit='saveEdit' :formData="editData"></edit-dialog>
    </el-dialog>
</div>
</template>
<script>
import {
  queryUseres,
  addSysFleetUser,
  editSysFleetUser,
  delSysFleetUser,
  resetSysUserpwd,
  // codeTableListb
} from '@/request/api'
import resizeMixin from '@/mixins/resize'
import isAuth from '@/mixins/buttonAuth'
import paginationMixin from '@/mixins/pagination'
import addDialog from './add'
import editDialog from './edit'
import buttonName from "@/components/buttonName/button-name";
// import isAuth from '@/mixins/buttonAuth'

export default {
  name: 'router.supplierAccountNumber',
  mixins: [resizeMixin, paginationMixin,isAuth],
  components:{
    addDialog,
    editDialog,
    buttonName
  },
  data(){
    return {
      form:{
        usrName:'',
        cmpName:'',
        //fltUuid:'-1'
      },
      lastModifier:this.$store.state.user.USERNAME_DX_H,
      addform:{
        cmpId:'', //必填
        cmpName:'',
        usrCode:'', //必填
        usrName:'', //必填
        usrStatus:'', //必填
        usrCellphone:'',
        usrEmail:'',
        remark:'',
        fltUuid:'-1', //必填
        creator:this.$store.state.user.USERNAME_DX_H,
        PageIndexs:'',
        PageSizes:''
      },
      //分页数据
      PageIndexs:'',
      PageSizes:'',
      //表格数据
      MainTableData:[],
      //选中行数据
      selectData:[],
      dialogFormVisible:false,
      //dialog模式
      mode:true,
      titlle:'',
      maxHeight:0,
      editData:[]
    }
  },
  mounted(){
  //   this.submitForm('form')
    // this.resize = () => {
    //   this.maxHeight = this.$refs.inner.offsetHeight - 40
    // }
  },
  methods:{
    clickEventDom(item) {
      if (item.mdlCode === 'add') {
        this.add()
      } else if (item.mdlCode === 'edit') {
        this.edit()
      } else if (item.mdlCode === 'del') {
        this.del()
      } else if (item.mdlCode === 'restPassword') {
        this.resetpwd()
      }
    },
  //密码重置
    resetpwd(){
      var {uuid,version} =this.selectData
      // console.log({uuid,version},141);

      if(this.selectData.length!=0){

        resetSysUserpwd({uuid,version}).then(res=>{
          if(res.success=='1'){
            this.$alert('密码重置成功！重置为Abc12345','提示',{
              confirmButtonText: '确定'
            })
            this.selectData=''
            return
          }else{
            this.$alert('密码重置失败！请稍后再试','提示',{
              confirmButtonText: '确定'
            })
            this.selectData=''
            return
          }
        })

      }else{
        this.$alert('请选择需要重置密码的数据！', '提示', {
          confirmButtonText: '确定'
        })
        return
      }
    },
    //查询按钮
    submitForm(form){
      this.$refs[form].validate(valid => {
        if(valid){
          this.refreshList()
        }
      })
    },
    //获取列表数据
    getList(currentPage, pageSize, updateTotal) {
      this.currentCodeList = []
      queryUseres({
        cmpName:this.form.cmpName,
        usrName:this.form.usrName,

        // rvDomain:this.copyDomain,
        // rvName:this.copyName,
        PageIndex: this.Pagination.currentPage.toString(),
        PageSize: this.Pagination.pageSize.toString()
      }).then(response => {
        this.MainTableData = response.data
        updateTotal(Number(response.total))
      })
      this.selectData = ''
    },
    //选中行事件
    rowClick(row){
      //console.log(row);
      this.selectData=row

      this.editData=JSON.parse(JSON.stringify(this.selectData))

      // console.log(this.editData,206)
    },
    //打开新增账号对话框
    add(){
      this.mode=true //显示新增页面
      this.title='新增账号'
      this.dialogFormVisible=true
    },
    //打开编辑账号对话框
    edit(){
      if(this.editData.length!=0){
        this.mode=false//显示编辑页面
        this.title='编辑账号'
        // console.log(this.editData)
        this.dialogFormVisible=true
      }else{
        this.$alert('请选择需要编辑的数据！', '提示', {
          confirmButtonText: '确定'
        })
        return
      }
    },
    //取消
    cancel(){
      if(Object.keys(this.editData).length!==0){
        this.editData=''
      }
      this.getList()
      this.dialogFormVisible=false
    },
    //保存
    save(data){
    // console.log(data)
      this.addform=data
      // console.log(this.addform,194);
      //console.log(addSysFleetUser)
      addSysFleetUser(this.addform).then(res=>{
        //console.log(res,1111);
        if(res.success==1){
          this.$message({
            message:'添加成功',
            type:'success'
          })
          this.dialogFormVisible=false
          this.submitForm('form')
        }else{
          this.$message({
            message:'添加失败，请稍后重试',
            type:'success'
          })
        }
      }).catch(err=>{
        console.log(err)
      })


      //this.getList();
    },
    //保存编辑
    saveEdit(data){
      var lastmodifier=this.lastModifier
      var val=Object.assign(data,{lastmodifier})
      // console.log(val,257)
      editSysFleetUser(val).then(res=>{
        if(res.success==1){
          this.$message({
            message:'编辑成功',
            type:'success'
          })
          this.editData=''
          this.dialogFormVisible=false
          //刷新列表
          this.getList()
        }else{
          this.$message({
            message:'编辑失败，请稍后重试',
            type:'warning'
          })
          this.editData=''
        }
      })
    },
    //删除
    del(){
      var lastModifier=this.lastModifier
      var valid=Object.assign(this.selectData,{lastModifier})
      if (Object.keys(this.selectData).length == 0) {
        this.$alert('请选择需要删除的数据！', '提示', {
          confirmButtonText: '确定'
        })
        return
      }
      this.$confirm('您确认要删除选定的数据吗?', '提示', {
        closeOnClickModal: false
      }).then(() => {

        delSysFleetUser(valid).then(() => {
          this.$message({
            message: '删除成功！',
            type: 'success',
            showClose: true
          })
          this.getList()
        })
      })

      // if(this.selectData.length!=0){
      //   var valid=Object.assign(this.selectData,{lastModifier});
      //   delSysFleetUser(valid).then(res=>{
      //     if(res.success==1){
      //       this.$message({
      //         message:'删除成功',
      //         type:'success'
      //       })
      //       // this.selectData='';
      //       this.submitForm('form');
      //     }else{
      //       this.$message({
      //         message:'删除失败，请稍后重试',
      //         type:'warning'
      //       })
      //       this.selectData='';
      //     }
      //   })
      // }else{
      //   this.$alert('请选择需要删除的数据！', '提示', {
      //     confirmButtonText: '确定'
      //   });
      //   return
      // }
    }
  },
  created() {
    // this.getList()
    this.refreshList()
    this.resize = () => {
      this.maxHeight = this.$refs.inner.offsetHeight - 40
    }
  }
}
</script>
<style lang="scss" scoped>
.sysuser{
  padding-top:15px;
  height:100%;
  display:flex;
  flex-direction: column;
  .btn-area{
      padding-bottom:15px;
      padding-left:15px;
  }
  .sysuser-table{
    padding-left: 15px;
    flex:1;
    // min-height: 300px;
    // display:flex;
    // flex-direction: column;
    // justify-items: space-between;
    .inner{
      height:100%;
      width: 100%;
      box-sizing:border-box;
    }
  }
  .block {
    padding-bottom: 5px;
    padding-right: 15px;
  }
}


</style>
